package com.fairytask.engine.components.text
{
	import com.fairytask.data.BaseData;
	import com.fairytask.engine.components.Scroll.ScrollPanel;
	import com.fairytask.engine.components.Scroll.ScrollSkin;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TextEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.geom.Rectangle;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * 滚动条文字 
	 * @author hanjy
	 * 
	 */	
	public class ScrollText extends Sprite
	{
		private 	var _txt			:RichTextField;
		private	var scrollPanel		:ScrollPanel;
		private 	var _scrollVal		:Number	=	1;
		
		//属性
		private 	var initHeight		:uint;
		private 	var initWidth		:uint;
		
		private var _srollSkin			:ScrollSkin;
		
		private var data				:BaseData	=	new BaseData();
		private var _color:String		=	"ffffff";
		
		public var isLink				:Boolean	=	false;
		
		private var _isLoaded			:Boolean	=	false;
		private var _preTxt			:String	=	"";
		
		private var _hasScroll			:Boolean	=	true;
		private var _canBreak			:Boolean	=	false;
		
		public static var TEXTCOMPONT	:String	=	"TEXTCOMPONT";
		
		public function ScrollText(w:uint,h:uint,vScrollSkin:String,hScrollSkin:String,pIconData:String,pHasScroll:Boolean=true)
		{
			super();
			
			initHeight	=	h;
			initWidth	=	w;
			_hasScroll	=	pHasScroll;
			if(_hasScroll)
			{
				_srollSkin	=	new ScrollSkin(vScrollSkin, hScrollSkin);
			}
			
			var tmpLoader	:URLLoader	=	new URLLoader();
			tmpLoader.addEventListener(Event.COMPLETE,onLoaded);
			tmpLoader.load(new URLRequest(pIconData));
		}
		
		/**
		 * 加载完毕 
		 * @param evt
		 * 
		 */
		private function onLoaded(evt:Event):void
		{
			_isLoaded	=	true;
			init(initWidth,initHeight);
			_txt.configXML	=	new XML(evt.target.data);
			if(null!=_preTxt&&""!=_preTxt)
			{
				htmlStr	=	_preTxt;
			}
			setFilter();
			if(data.CheckProperties("onLinkHandle"))
			{
				_txt.addEventListener(TextEvent.LINK,onLinkHandle);
				_txt.addEventListener(Event.RESIZE,onReSize);
			}
		}
		
		/**
		 * 初始化显示 
		 * @param w
		 * @param h
		 * @param scrollSkin
		 * 
		 */
		private function init(w:uint,h:uint):void
		{
			_txt					=	new RichTextField(w-10,h);
			_txt.textField.multiline=	true;
			_txt.textField.wordWrap	=	true;
			_txt.textField.selectable	=	false;
			//_txt.textField.mouseWheelEnabled=	false;
			_txt.textField.textColor		=	uint("0x"+_color);
			
			if(_hasScroll)
			{
				scrollPanel	=	new ScrollPanel(new Rectangle(0,0,w,h),_txt,_srollSkin);
				addChild(scrollPanel);
			}
			else
			{
				_txt.textField.width	=	w;
				_txt.textField.height	=	h;
				addChild(_txt);
			}
			setFilter();
			
			//事件监听
			//scrollPanel.addEventListener(MouseEvent.CLICK, onScrollClick);
		}
		
		/**
		 * 设置文字名称，用于场景 
		 * @param val
		 * 
		 */
		public function set txtName(val:String):void
		{
			//_txt.textField.name	=	val;
		}
		
		public function setFilter():void
		{
			var glow:GlowFilter		= 	new GlowFilter();
			glow.color 				= 	0x000000;
			glow.alpha 				= 	1;
			glow.blurX 				=	2;
			glow.blurY				= 	2;
			glow.strength			=	6;
			glow.quality			= 	BitmapFilterQuality.LOW;
			_txt.textField.filters		=	[glow];
		}
		
		public function set htmlStr(val:String):void
		{
			if(_isLoaded)
			{
				if(val!=null)
				{
					if(_canBreak)
					{
						_txt.clear();
						var i:int;
						var j:int;
						var str:String="";
						var tf:RichTextField=new RichTextField(initWidth+100,initHeight);
						var font:TextFormat=	new TextFormat();
						var tmpStr:String="";
						font.font	=	"微软雅黑";
						tf.textField.wordWrap=false;
						tf.textField.multiline=false;
						tf.textField.autoSize=TextFieldAutoSize.LEFT;
						tf.textField.defaultTextFormat=font;
						for(i=0;i<val.length;i++)
						{
							//html标签处理
							if(val.charAt(i)=="<")
							{
								j=val.indexOf(">",i);
								if(str=="")
								{
									str=val.substring(i,j+1);
								}
								else
								{
									str+=val.substring(i,j+1);
									if(val.substring(i,j+1)=="<br>"||val.substring(i,j+1)=="</P>")
									{
										tf.textField.htmlText="";
										tmpStr="";
									}
								}
								i=j;
							}
								//内容处理
							else
							{
								tmpStr+=val.charAt(i);
								tf.textField.htmlText=tmpStr;
								//超过宽度强制换行
								if(tf.width>(initWidth-10)&&val.substr(i+1,4)!="<br>"&&val.substr(i+1,11)!="</FONT></P>")
								{
									tf.textField.htmlText="";
									tmpStr="";
									str+="<br>";
								}
								str+=val.charAt(i);
							}
						}
						_txt.richText=str;
						tf.textField.htmlText="";
						tmpStr="";
					}
					else
					{
						_txt.richText	=	val;
					}
				}
				//trace("文字高度是多少:",_txt.height,_txt.textField.height,_txt.textField.textHeight);
			}
			else
			{
				_preTxt	=	val;
			}
			onTextChange();
		}
		
		public function get htmlStr():String
		{
			return _txt.richText;
		}
		
		/**
		 * 清除文字显示与文字区域 
		 */
		public function clear():void
		{
			_txt.clear();
			onTextChange();
		}
		
		/**
		 * 文字链接点击事件 
		 * @param val
		 */
		public function addLinkClickHandel(val:Function):void
		{
			data.HandleProperties("onLinkHandle",val);
			if(_isLoaded)
			{
				_txt.addEventListener(TextEvent.LINK,onLinkHandle);
			}
		}
		
		public function removeLinkClickHandel(val:Function):void
		{
			_txt.removeEventListener(TextEvent.LINK,onLinkHandle);
		}
		
		private function onLinkHandle(e:TextEvent):void
		{
			var tFunc:Function;
			
			if(data.CheckProperties("onLinkHandle"))
			{
				isLink	=	true;
				tFunc	=	data.GetProperties("onLinkHandle") as Function;
				tFunc.call(this,e.text);
				tFunc	=	null;
			}
		}
		
		private function onReSize(e:Event):void
		{
			onTextChange();
		}
		
		/**
		 * 移除文字和事件 
		 */
		public function remove():void
		{
			_txt.removeEventListener(TextEvent.LINK,onLinkHandle);
			data.DeleteProperties("onLinkHandle");
			if(this.parent != null)
			{
				this.parent.removeChild(this);
			}
		}
		
		public function onTextChange():void
		{
			if(null!=scrollPanel&&_scrollVal!=-1)
			{
				scrollPanel.update(_scrollVal);
			}
		}
		
		/**
		 * 设定滚动条位置 
		 * @param value
		 */
		public function set scrollVal(value:Number):void
		{
			_scrollVal = value;
		}
		
		/**
		 * 强制换行
		 * @param value
		 */
		public function set canBreak(value:Boolean):void
		{
			_canBreak = value;
		}
		
		/**
		 * 设置滚动条从顶端显示
		 * @param bool
		 */
		public function set showTop(bool:Boolean):void
		{
			if(null!=scrollPanel&&bool)
			{
				scrollPanel.cTcY=0;
				scrollPanel.update(0);
			}
		}
		
		/**
		 * 重设大小 
		 * @param width
		 * @param height
		 * 
		 */
		public function resize(width:int,height:int):void
		{
			_txt.resizeTo(width-10,height);
			initWidth=width;
			initHeight=height;
			if(null!=scrollPanel)
			{
				scrollPanel.resize(new Rectangle(0,0,width,height));
			}
			if(_canBreak&&_txt.htmlTxt!=null)
			{
				var _str:String=_txt.htmlTxt;
				_str=_str.replace("<br>","")
				this.htmlStr=_str;
			}
			else
			{
				this.htmlStr=_txt.htmlTxt;
			}
			onTextChange();
		}
	}
}